<template>
  <div>
    <el-steps :active="active" finish-status="success">
      <el-step title="账户注册"></el-step>
      <el-step title="信息填写"></el-step>
      <el-step title="账户激活"></el-step>
    </el-steps>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model.number="ruleForm.username" @blur="uniqueUser"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
<!--        <el-button  @click="submitForm('ruleForm')">提交</el-button>-->
        <el-button type="primary" style="margin-top: 12px;" @click="next">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
export default {
  name: "Register.vue",
  data() {

    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
      setTimeout(() => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
            callback();
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      active: 0,
      isUnique: false,
      ruleForm: {
        password: '',
        checkPass: '',
        username: '',

      },
      rules: {
        password: [
          {required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          {required: true, validator: validatePass2, trigger: 'blur' }
        ],
        username: [
          {required: true, validator: checkUsername, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    /**
     * 唯一性用户名验证
     */
    uniqueUser(){
      const _this=this
      this.$axios.post("/uniqueUsername/"+this.ruleForm.username).then(res =>{
        if(res.data.data===true){
          this.isUnique=true
          this.$message({
            showClose: true,
            message: '用户名可用',
            type: 'success'
          });
        }else{
          debugger
          this.isUnique=false
          this.$message.error("用户名不可用!");
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    next() {
      if(!this.isUnique){
        this.$message.error("用户名不可用!");
        return ;
      }
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const _this=this
          this.$axios.post("/registerLogin",this.ruleForm).then(res =>{
            if(res.data.code===200){
              this.$message({
                showClose: true,
                message: '账户信息填写成功',
                type: 'success'
              });
              _this.$router.push("/registerInfo/"+res.data.data.userId)
            }else{
              this.$message.error(res.data.message);
            }
          })
        }
      });
    }
  }

}
</script>
<style>

</style>